package fun.chutianshu;

import java.time.Duration;
import java.time.LocalDateTime;
import java.util.StringJoiner;

public class Main {

    public static void main(String[] args) {
        // 例子1
        //stringBuilderTest();
        //例子2
        //example02();
        //例子3
        example03();
    }

    //例子3
    static void example03(){
        String[] fields = { "name", "position", "salary" };
        String table = "employee";
        String select = buildSelectSql(table, fields);
        System.out.println(select);
        System.out.println("SELECT name, position, salary FROM employee".equals(select) ? "测试成功" : "测试失败");
    }
    //例子3中使用到的拼接sql的方法
    static String buildSelectSql(String table, String[] fields) {
        // TODO:
        StringJoiner sj = new StringJoiner(", ","SELECT "," FROM "+table);
        for(String s:fields)
        {
            sj.add(s);
        }
        String s = sj.toString();
        return s;
    }

    //例子2，进行拼接操作联系
    static void example02(){
        String[] fields = { "name", "position", "salary" };
        String table = "employee";
        String insert = buildInsertSql(table, fields);
        System.out.println("拼接后的 sql:\n"+insert);
        String s = "INSERT INTO employee (name, position, salary) VALUES (?, ?, ?)";
        System.out.println("原sql： \n"+s);
        System.out.println(s.equals(insert) ? "测试成功" : "测试失败");

    }

    //例子2中使用到的拼接sql的方法
    static String buildInsertSql(String table, String[] fields) {
        // TODO:
        StringBuilder sb = new StringBuilder(1024);
        sb.append("INSERT INTO ").
                append(table).
                append(" (");
        for(int i=0;i<fields.length;i++)
        {
            sb.append(fields[i]);
            if(i!=fields.length-1) {
                sb.append(", ");
            }
        }
        sb.append(") VALUES (");
        for(int i = 0;i<fields.length;i++)
        {
            sb.append("?");
            if(i!=fields.length-1) {
                sb.append(", ");
            }
        }
        sb.append(")");

        return sb.toString();
    }

    //例子1：测试 StringBuilder 运行效率
    public static void stringBuilderTest(){
        //两种处理方式：1. 系统时间; 2.LocalDateTime 计算时间日期
        // System.currentTimeMillis() 将系统时间转换为长整型，单位是 毫秒（千分之一秒）
        // long time1 = System.currentTimeMillis();
        //使用 LocalDateTime 类的静态方法 now() 获取当前日期时间
        LocalDateTime begin =  LocalDateTime.now();
        System.out.println("开启时间：  "+begin);
        // write your code here
        String s = "";
        for (int i = 0; i < 10000; i++) {
            s = s + "," + i;
        }
        System.out.println(s);
        LocalDateTime end =  LocalDateTime.now();
        System.out.println("结束时间：  "+end);

        //时间间隔类，用来存储两个 日期时间类 对象的时间差
        Duration duration = Duration.between(begin,end);

        //时间间隔类 有很多转换方法，可以转换为 年、月、星期、日、分钟、秒、毫秒、微秒
        long millis = duration.toMillis();//相差毫秒数

        System.out.println("使用 + 连接字符串，共耗时："+millis+"毫秒");

        System.out.println();
        System.out.println("#########################################");
        System.out.println();

        begin =  LocalDateTime.now();
        System.out.println("开启时间：  "+begin);
        StringBuilder sb = new StringBuilder(1024);
        for (int i = 0; i < 10000; i++) {

            //sb.append(',');
            //sb.append(i);
            sb.append(",").append(i);
        }
        String s1 = sb.toString();
        System.out.println(s1);
        end =  LocalDateTime.now();
        System.out.println("结束时间：  "+end);

        duration = Duration.between(begin,end);
        millis = duration.toMillis();//相差毫秒数
        System.out.println("使用 StringBuilder 连接字符串，共耗时："+millis+"毫秒");
    }
}
